package edu.zut.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//利用@WebServlet注解自动配置url与servlet的映射
@WebServlet(name = "PVServlet", urlPatterns = {"/pv"})
public class PVServlet extends HttpServlet {
    /**
     * web容器支持单实例多线程模式，意味着我们的servlet实例对象在整个web容器中，就只有一个
     */
    //count表示当前pv请求的访问次数
    int count = 0;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        count++;
        System.out.println("count = " + count);
        response.getWriter().write("<html>\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <title>首页</title>\n" +
                "</head>\n" +
                "<body>\n" +
                "点击次数为： " + count +
                "</body>\n" +
                "</html>\n");

    }
}
